//
//  DataUser.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/10/6.
//

import Foundation

// MARK: - DataCall
struct DataCall: Codable {
    let orderType: Int
    var receiverJSON:String = ""
    var fanwei : Int
    let lon, lat : Double
    let lon2,lat2: Double
    let from, openid, phone, laosPhone: String
    let arriveTime :Int64
    let sex: Int
    var hujiao:Int = 0
    var id : String = "0"
    let need : String = "呼叫车辆"
    let address : String = "from"
    let endAddress: String = "to"
    let peoples : Int = 1
    var ownername = ""
    let ownerheadavitor = "1.png"
    let distance = 20
    let status = 0
    var ownerphone=""
    let time : Int64
    let nowPeoples : Int
    let country = 1
   var cityCode = "1"

    enum CodingKeys: String, CodingKey {
        case orderType = "order_type"
        case receiverJSON, fanwei, lon, lat, lon2, lat2, from, openid, phone,ownerphone
        case laosPhone = "laos_phone"
        case arriveTime = "arrive_time"
        case sex
        case hujiao
        case id
        case need,address
        case peoples
        case endAddress = "end_address"
        case ownername,ownerheadavitor
        case distance
        case status
        case time
        case nowPeoples = "now_peoples"
        case country
       case cityCode = "city_code"
    }
    
    func toUrlParams() -> String {
       if lon2 > 0 && lat2 > 0 {
          return "order_type=\(orderType)&receiverJSON=&fanwei=\(fanwei)&lon=\(lon)&lat=\(lat)&lon2=\(lon2)&lat2=\(lat2)&from=\(from)&openid=\(openid)&ownerphone=\(ownerphone)&laos_phone=\(laosPhone)&arrive_time=\(arriveTime)&sex=\(sex)&hujiao=\(hujiao)&id=\(id)&need=\(need)&address=\(address)&peoples=\(peoples)&end_address=\(endAddress)&ownername=\(ownername)&ownerheadvitor=\(ownerheadavitor)&distance=\(distance)&status=\(status)&time=\(time)&now_peoples=\(nowPeoples)&country=\(country)&city_code=\(cityCode)"
       } else {
          return "order_type=\(orderType)&receiverJSON=&fanwei=\(fanwei)&lon=\(lon)&lat=\(lat)&lon2=&lat2=&from=\(from)&openid=\(openid)&ownerphone=\(ownerphone)&laos_phone=\(laosPhone)&arrive_time=\(arriveTime)&sex=\(sex)&hujiao=\(hujiao)&id=\(id)&need=\(need)&address=\(address)&peoples=\(peoples)&end_address=\(endAddress)&ownername=\(ownername)&ownerheadvitor=\(ownerheadavitor)&distance=\(distance)&status=\(status)&time=\(time)&now_peoples=\(nowPeoples)&country=\(country)&city_code=\(cityCode)"
       }
    }
    
    func toJSONString(receivers:[ReceiverJSON]) -> String {
        var s = "["
        receivers.forEach { json in
            s =  s + "{\"peopleId\":\"\(json.peopleID)\"},"
        }
        s.removeLast()
        s = s + "]"
        print("toJSON:" + s)
        return s
    }
    
    init() {
        self.orderType = 1
        let openId = ViewModel.shared.passenger.openId
        var receivers = [ReceiverJSON]()
        let receiver = ReceiverJSON(peopleID: openId)
        receivers.append(receiver)
        self.receiverJSON = "[{\"peopleId\":\"\(openId)\"}]"
        self.fanwei = 10
        
        self.lon = Constants.defaultLongitude
        self.lat = Constants.defaultLatitude
        self.lon2 = 0
        self.lat2 = 0
        self.from = openId
        self.openid = openId
        self.phone = ViewModel.shared.passenger.mobile
        self.laosPhone=ViewModel.shared.passenger.laosMobile
        self.ownername = ViewModel.shared.passenger.name
        self.ownerphone = ViewModel.shared.passenger.mobile
        self.arriveTime = Date().secondsSince1970
        self.time = self.arriveTime
        self.sex = 1
        self.nowPeoples = 1
        self.fanwei = 3
    }

   init(lon:Double,lat:Double,lon2:Double,lat2: Double, phone:String,wechat:String,peopleNum:Int) {
        let openId = ViewModel.shared.passenger.openId
        self.orderType = 1
        var receivers = [ReceiverJSON]()
        let receiver = ReceiverJSON(peopleID: openId)
        receivers.append(receiver)
        self.receiverJSON = "[{\"peopleId\":\"\(openId)\"}]"
        self.fanwei = 10

        self.lon = lon
        self.lat = lat
        self.lon2 = lon2
        self.lat2 = lat2
        self.from = openId
        self.openid = openId
        self.phone = ViewModel.shared.passenger.mobile
        self.laosPhone=ViewModel.shared.passenger.laosMobile
        self.ownername = ViewModel.shared.passenger.name
        self.ownerphone = ViewModel.shared.passenger.mobile

        self.arriveTime = Date().secondsSince1970
        self.time = self.arriveTime
        self.sex = 1
        self.nowPeoples = peopleNum
    }
    
    init(lon:Double,lat:Double,lon2:Double,lat2: Double, phone:String,wechat:String,peopleNum:Int,fanwei:Int) {
         let openId = ViewModel.shared.passenger.openId
         self.orderType = 1
         var receivers = [ReceiverJSON]()
         let receiver = ReceiverJSON(peopleID: openId)
         receivers.append(receiver)
         self.receiverJSON = "[{\"peopleId\":\"\(openId)\"}]"
         self.fanwei = 10

         self.lon = lon
         self.lat = lat
         self.lon2 = lon2
         self.lat2 = lat2
         self.from = openId
         self.openid = openId
         self.phone = ViewModel.shared.passenger.mobile
         self.laosPhone=ViewModel.shared.passenger.laosMobile
         self.ownername = ViewModel.shared.passenger.name
         self.ownerphone = ViewModel.shared.passenger.mobile

         self.arriveTime = Date().secondsSince1970
         self.time = self.arriveTime
         self.sex = 1
         self.nowPeoples = peopleNum
         self.fanwei = fanwei
     }
}

// MARK: - ReceiverJSON
struct ReceiverJSON: Codable {
    let peopleID: String
    
    enum CodingKeys: String, CodingKey {
        case peopleID = "peopleId"
    }
}
